package transformation;

import hulppakket.Vector3f;

public class Rotation extends Matrix4f {

	private static final long serialVersionUID = 5197568951845784946L;

	public Rotation(Vector3f axis, float angle) {
		Vector3f u, v, w, t;
		float sin, cos;
		angle = (float) Math.toRadians(angle);
		sin = (float) Math.sin(angle);
		cos = (float) Math.cos(angle);
		w = axis.normalize();
		t = this.getT(w);
		u = t.crossProduct(w).normalize();
		v = w.crossProduct(u);
		Matrix4f m1 = new Matrix4f(u.x, v.x, w.x, 0, u.y, v.y, w.y, 0, u.z,
				v.z, w.z, 0, 0, 0, 0, 1);
		Matrix4f m2 = new Matrix4f(cos, -sin, 0, 0, sin, cos, 0, 0, 0, 0, 1, 0,
				0, 0, 0, 1);
		Matrix4f m3 = new Matrix4f(u.x, u.y, u.z, 0, v.x, v.y, v.z, 0, w.x,
				w.y, w.z, 0, 0, 0, 0, 1);
		this.set(m1.multiply(m2.multiply(m3)));
	}

	private Vector3f getT(Vector3f w) {
		Vector3f result = new Vector3f(w);
		if (w.x < w.y && w.x < w.z) {
			result.x = 1;
		} else if (w.y < w.x && w.y < w.z) {
			result.y = 1;
		} else {
			result.z = 1;
		}
		return result;
	}

}
